package com.shengqi.xinnongren.work.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.utils.ToastHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ChooseItemDialog extends Dialog {

    public ChooseItemDialog(@NonNull Context context) {
        super(context);
    }

    public ChooseItemDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置dialog 窗体的宽度
        if (getWindow() != null) {
            getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
            getWindow().getAttributes().gravity = Gravity.CENTER_VERTICAL;
        }
    }

    public static class Builder {
        private Context mContext;

        private List<String> mStringList;
        private ChooseItemAdapter mAdapter;
        private OnItemClickListener mListener;
        private ChooseItemDialog dialog;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setStringList(List<String> stringList) {
            mStringList = stringList;
            return this;
        }

        public Builder setOnItemClickListener(OnItemClickListener listener) {
            mListener = listener;
            return this;
        }

        public ChooseItemDialog build() {

            dialog = new ChooseItemDialog(mContext, R.style.CustomDialog);

            View layout = View.inflate(mContext, R.layout.layout_add_address_dialog, null);

            RecyclerView recyclerView = layout.findViewById(R.id.rv_list);
            mAdapter = new ChooseItemAdapter(mStringList);
            recyclerView.setAdapter(mAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

            //将布局添加到 dialog 中
            dialog.setContentView(layout);
            return dialog;
        }

        public class ChooseItemAdapter extends RecyclerView.Adapter<ChooseItemAdapter.Holder> {

            List<String> mDataList;

            public ChooseItemAdapter(List<String> dataList) {
                mDataList = dataList;
            }

            @NonNull
            @Override
            public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                Holder holder = new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_dialog_choose, parent, false));
                holder.itemView.setOnClickListener(v -> {
                    if (mDataList.get(holder.getAdapterPosition()).split("#")[1].contains("1")) {
                        ToastHelper.getInstance(mContext).showWarn("该时间段已满");
                        return;
                    }
                    if (mListener != null) {
                        mListener.onItemClick(dialog, holder.getAdapterPosition());
                    }
                });
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull Holder holder, int position) {
                String[] split = mDataList.get(position).split("#");
                holder.ivFull.setVisibility(split[1].contains("1") ? View.VISIBLE : View.GONE);
                holder.tvTime.setText(split[0]);
            }

            @Override
            public int getItemCount() {
                return mDataList.size();
            }

            public class Holder extends RecyclerView.ViewHolder {
                @BindView(R.id.item_time)
                TextView tvTime;
                @BindView(R.id.iv_full)
                ImageView ivFull;

                public Holder(View itemView) {
                    super(itemView);
                    ButterKnife.bind(this, itemView);
                }
            }
        }

        public interface OnItemClickListener {
            void onItemClick(ChooseItemDialog dialog, int position);
        }
    }
}
